DI container should respect generic constraints when resolving single instance#123255
Merged
rosebyte merged 3 commits intodotnet:mainfrom Jan 23, 2026
Merged
DI container should respect generic constraints when resolving single instance#123255rosebyte merged 3 commits intodotnet:mainfrom
rosebyte merged 3 commits intodotnet:mainfrom
Conversation
Member
Author
|
@dotnet-policy-service agree |
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the DI container where resolving a single service instance (GetService) could incorrectly return an earlier compatible open generic registration when the most recent registration had incompatible generic constraints, violating the "last wins" principle.
Changes:
- Updated
CallSiteFactoryto track the slot counter even when the last open generic registration has incompatible constraints, ensuring single service resolution correctly attempts to use the last registration and throwsArgumentException - Added a test case to verify that incompatible constrained open generics are skipped in enumerable resolution but cause exceptions in single service resolution
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs | Updated slot tracking logic to include the case where the last registration has incompatible constraints, ensuring proper "last wins" semantics |
| src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs | Added test case to verify the fix works correctly for both enumerable and single service resolution scenarios |
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs
Outdated
Show resolved
Hide resolved
...ies/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs
Outdated
Show resolved
Hide resolved
...ies/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs
Outdated
Show resolved
Hide resolved
cincuranet
approved these changes
Jan 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #108874
DI filters out open generics with incompatible constraints by default. However, if the most recently registered open generic has incompatible constraints, DI skips it during IEnumerable resolution and falls back to the previous compatible registration. If that earlier registration is compatible, DI caches it as the “last” valid one. As a result, when users resolve a service whose most recent implementation had incompatible constraints, but an earlier one was compatible, they receive an implementation that is not the last registered. This breaks the usual “last wins” rule of DI containers and can lead to confusing, nearly impossible to debug behaviour.